package com.beehoo.dcportal.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beehoo.dcportal.admin.dao.MessageTypeDao;
import com.beehoo.dcportal.admin.service.MessageTypeService;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.common.base.BaseService;
import com.beehoo.dcportal.model.MenuBean;
import com.beehoo.dcportal.utils.CommonUtils;

public class MessageTypeServiceImpl extends BaseService implements
		MessageTypeService {

	private MessageTypeDao messageTypeDao;

	public void setMessageTypeDao(MessageTypeDao messageTypeDao) {
		this.messageTypeDao = messageTypeDao;
	}

	@Override
	public String createMessageType(MenuBean menuBean) throws BaseException {
		validateMessageTypeBean(menuBean);
		if(menuBean.getParentMenu().equals("")){
			menuBean.setMenuLevel("1");
		}else{
			menuBean.setMenuLevel("2");
		}
		return messageTypeDao.createMessageType(menuBean);
	}

	@Override
	public int delMessageType(String typeId) throws BaseException {
		if (CommonUtils.nvlToString(typeId).equals("")) {
			throw new BaseException(
					"custom.messages.error.message.type.id.empty");
		}
		return messageTypeDao.delMessageType(typeId);
	}

	@Override
	public MenuBean queryMessageType(String streetId, String typeId)
			throws BaseException {
		if (CommonUtils.nvlToString(streetId).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		if (CommonUtils.nvlToString(typeId).equals("")) {
			throw new BaseException(
					"custom.messages.error.message.type.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		paramMap.put("menuId", typeId);
		return messageTypeDao.queryMessageType(paramMap);
	}

	@Override
	public List<MenuBean> queryMessageTypeLs(String streetId, String menuLevel,
			String parentMenu) throws BaseException {
		if (CommonUtils.nvlToString(streetId).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		paramMap.put("menuLevel", menuLevel);
		paramMap.put("parentMenu", parentMenu);
		return messageTypeDao.queryMessageTypeLs(paramMap);
	}
	
	@Override
	public List<MenuBean> queryMessageTypeLs(String streetId, String menuLevel,
			String parentMenu, String menuSpecial) throws BaseException {
		if (CommonUtils.nvlToString(streetId).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		paramMap.put("menuLevel", menuLevel);
		paramMap.put("parentMenu", parentMenu);
		paramMap.put("menuSpecial", menuSpecial);
		return messageTypeDao.queryMessageTypeLs(paramMap);
	}

	@Override
	public int updateMessageType(MenuBean menuBean) throws BaseException {
		validateMessageTypeBean(menuBean);
		if(CommonUtils.nvlToString(menuBean.getParentMenu()).equals("")){
			menuBean.setMenuLevel("1");
		}else{
			if(menuBean.getParentMenu().equals(menuBean.getMenuId())){
				throw new BaseException(
						"custom.messages.error.message.type.update.error");
			}
			menuBean.setMenuLevel("2");
		}
		return messageTypeDao.updateMessageType(menuBean);
	}

	private void validateMessageTypeBean(MenuBean menuBean)
			throws BaseException {
		if (menuBean == null) {
			throw new BaseException(
					"custom.messages.error.message.type.bean.empty");
		}
		if (CommonUtils.nvlToString(menuBean.getStreetId()).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		if (CommonUtils.nvlToString(menuBean.getMenuName()).equals("")) {
			throw new BaseException(
					"custom.messages.error.message.type.name.empty");
		}
	}

	@Override
	public void initStreetMenu(String streetId) throws BaseException {
		if (CommonUtils.nvlToString(streetId).equals("")) {
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		Map<String, Object> paramMapLeve1 = null;
		
		//初始化一级导航菜单
		String[] menuName_level1 = 
			{"中心简介","中心动态","党建园地","政务公开","政策信息"};
		String[] menuId_level1 = {"","","","",""};
		for(int i=0;i<menuName_level1.length;i++){
			MenuBean menuBeanLevel1 = new MenuBean();
			menuBeanLevel1.setStreetId(streetId);
			menuBeanLevel1.setMenuName(menuName_level1[i]);
			menuBeanLevel1.setParentMenuSpecial("");
			menuBeanLevel1.setParentMenu("");
			menuBeanLevel1.setMenuLevel("1");
			menuBeanLevel1.setMenuOrder(String.valueOf(i+1));
			menuBeanLevel1.setMenuPath("");
			menuBeanLevel1.setMenuShow("1");
			if(menuName_level1[i].equals("政务公开")){
				menuBeanLevel1.setMenuSpecial("010");
			}else{
				menuBeanLevel1.setMenuSpecial("000");
			}
			menuId_level1[i] = messageTypeDao.initStreetMenu(menuBeanLevel1);
		}
		
		//初始化二级导航菜单---中心简介
		String[] menuName_level2_1 = 
			{"中心简介","中心历史","中心纪事"};
		String[] menuId_level2_1 = {"","",""};
		for(int i=0;i<menuName_level2_1.length;i++){
			MenuBean menuBean = new MenuBean();
			menuBean.setStreetId(streetId);
			menuBean.setMenuName(menuName_level2_1[i]);
			menuBean.setMenuSpecial("000");
			menuBean.setParentMenuSpecial("");
			menuBean.setParentMenu(menuId_level1[0]);
			menuBean.setMenuLevel("2");
			menuBean.setMenuOrder(String.valueOf(i+1));
			menuBean.setMenuPath("");
			menuBean.setMenuShow("1");
			menuId_level2_1[i] = messageTypeDao.initStreetMenu(menuBean);
		}
		for(int i=0;i<menuId_level2_1.length;i++){
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("menuId", menuId_level2_1[i]);
			paramMap.put("menuPath", "msg/list/"+menuId_level2_1[i]+"/00/1");
			messageTypeDao.initStreetMenuPath(paramMap);
		}
		paramMapLeve1 = new HashMap<String, Object>();
		paramMapLeve1.put("menuId", menuId_level1[0]);
		paramMapLeve1.put("menuPath", "msg/list/"+menuId_level2_1[0]+"/00/1");
		messageTypeDao.initStreetMenuPath(paramMapLeve1);
		
		//初始化二级导航菜单---中心动态
		String[] menuName_level2_2 = 
			{"通知公告","中心新闻","工作动态"};
		String[] menuId_level2_2 = {"","",""};
		for(int i=0;i<menuName_level2_2.length;i++){
			MenuBean menuBean = new MenuBean();
			menuBean.setStreetId(streetId);
			menuBean.setMenuName(menuName_level2_2[i]);
			menuBean.setMenuSpecial("000");
			menuBean.setParentMenuSpecial("");
			menuBean.setParentMenu(menuId_level1[1]);
			menuBean.setMenuLevel("2");
			menuBean.setMenuOrder(String.valueOf(i+1));
			menuBean.setMenuPath("");
			menuBean.setMenuShow("1");
			menuId_level2_2[i] = messageTypeDao.initStreetMenu(menuBean);
		}
		for(int i=0;i<menuId_level2_2.length;i++){
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("menuId", menuId_level2_2[i]);
			paramMap.put("menuPath", "msg/list/"+menuId_level2_2[i]+"/00/1");
			messageTypeDao.initStreetMenuPath(paramMap);
		}
		paramMapLeve1 = new HashMap<String, Object>();
		paramMapLeve1.put("menuId", menuId_level1[1]);
		paramMapLeve1.put("menuPath", "msg/list/"+menuId_level2_2[0]+"/00/1");
		messageTypeDao.initStreetMenuPath(paramMapLeve1);
		
		//初始化二级导航菜单---党建园地
		String[] menuName_level2_3 = 
			{"中心党建","社区党建"};
		String[] menuId_level2_3 = {"",""};
		for(int i=0;i<menuName_level2_3.length;i++){
			MenuBean menuBean = new MenuBean();
			menuBean.setStreetId(streetId);
			menuBean.setMenuName(menuName_level2_3[i]);
			menuBean.setMenuSpecial("000");
			menuBean.setParentMenuSpecial("");
			menuBean.setParentMenu(menuId_level1[2]);
			menuBean.setMenuLevel("2");
			menuBean.setMenuOrder(String.valueOf(i+1));
			menuBean.setMenuPath("");
			menuBean.setMenuShow("1");
			menuId_level2_3[i] = messageTypeDao.initStreetMenu(menuBean);
		}
		for(int i=0;i<menuId_level2_3.length;i++){
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("menuId", menuId_level2_3[i]);
			paramMap.put("menuPath", "msg/list/"+menuId_level2_3[i]+"/00/1");
			messageTypeDao.initStreetMenuPath(paramMap);
		}
		paramMapLeve1 = new HashMap<String, Object>();
		paramMapLeve1.put("menuId", menuId_level1[2]);
		paramMapLeve1.put("menuPath", "msg/list/"+menuId_level2_3[0]+"/00/1");
		messageTypeDao.initStreetMenuPath(paramMapLeve1);
		
		//初始化二级导航菜单---政务公开
		String[] menuName_level2_4 = 
			{"领导介绍","组织机构","下辖社区"};
		String[] menuId_level2_4 = {"","",""};
		for(int i=0;i<menuName_level2_4.length;i++){
			MenuBean menuBean = new MenuBean();
			menuBean.setStreetId(streetId);
			menuBean.setMenuName(menuName_level2_4[i]);
			menuBean.setMenuSpecial("01"+String.valueOf(i+1));
			menuBean.setParentMenuSpecial("010");
			menuBean.setParentMenu(menuId_level1[3]);
			menuBean.setMenuLevel("2");
			menuBean.setMenuOrder(String.valueOf(i+1));
			menuBean.setMenuPath("");
			menuBean.setMenuShow("1");
			menuId_level2_4[i] = messageTypeDao.initStreetMenu(menuBean);
		}
		for(int i=0;i<menuId_level2_4.length;i++){
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("menuId", menuId_level2_4[i]);
			if(i==0)
				paramMap.put("menuPath", "leader/"+menuId_level2_4[i]);
			else if(i==1)
				paramMap.put("menuPath", "organization/"+menuId_level2_4[i]);
			else if(i==2)
				paramMap.put("menuPath", "community/"+menuId_level2_4[i]);
			messageTypeDao.initStreetMenuPath(paramMap);
		}
		paramMapLeve1 = new HashMap<String, Object>();
		paramMapLeve1.put("menuId", menuId_level1[3]);
		paramMapLeve1.put("menuPath", "leader/"+menuId_level2_4[0]);
		messageTypeDao.initStreetMenuPath(paramMapLeve1);
		
		//初始化二级导航菜单---政策信息
		String[] menuName_level2_5 = 
			{"就业政策","扶残政策","卫生计生"};
		String[] menuId_level2_5 = {"","",""};
		for(int i=0;i<menuName_level2_5.length;i++){
			MenuBean menuBean = new MenuBean();
			menuBean.setStreetId(streetId);
			menuBean.setMenuName(menuName_level2_5[i]);
			menuBean.setMenuSpecial("000");
			menuBean.setParentMenuSpecial("");
			menuBean.setParentMenu(menuId_level1[4]);
			menuBean.setMenuLevel("2");
			menuBean.setMenuOrder(String.valueOf(i+1));
			menuBean.setMenuPath("");
			menuBean.setMenuShow("1");
			menuId_level2_5[i] = messageTypeDao.initStreetMenu(menuBean);
		}
		for(int i=0;i<menuId_level2_5.length;i++){
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("menuId", menuId_level2_5[i]);
			paramMap.put("menuPath", "msg/list/"+menuId_level2_5[i]+"/00/1");
			messageTypeDao.initStreetMenuPath(paramMap);
		}
		paramMapLeve1 = new HashMap<String, Object>();
		paramMapLeve1.put("menuId", menuId_level1[4]);
		paramMapLeve1.put("menuPath", "msg/list/"+menuId_level2_5[0]+"/00/1");
		messageTypeDao.initStreetMenuPath(paramMapLeve1);
	}

}
